package com.ndp.fb.fbagent.service.impl;

import com.ndp.fb.fbagent.service.impl.paramadapter.FacebookParamAdapter;
import com.ndp.fb.fbagent.service.webrequest.AccountQueueWebRequestor;
import com.ndp.fb.model.fbagent.param.customaudience.CustomAudienceLookalikeParam;
import com.ndp.fb.model.fbagent.param.customaudience.LookalikeSpec;
import com.restfb.*;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by elvisxiong on 15/11/25.
 */
public class CreateCustomAudienceLike {
    public static final Version VERSION = Version.Version_2_6;
    FacebookParamAdapter facebookParamAdapter = new FacebookParamAdapter();

    String token = "CAAJA3QjSHaMBANGZCQD5LJNiS4VC7CpdAwJFKzZCe1v8dccjobZBgaEn5q6Kl1tNZCs6XCYNtgK5k9D5N5nAGMzm7km8mvqLhzwqdlDS2ZCuZCNQAX8KkZBK2AHCzp6BEwyyR5XMhZAZB96jUGthNGrth0TOxIRmoMa0gL23oWCsWRm52U0sHBa4ZAbSOc2XhAvkZA5PFgVBvfltCZA9WHZAi3Pj4";
    DefaultFacebookClient defaultFacebookClient = new DefaultFacebookClient(token, Version.Version_2_6);

    CustomAudienceLookalikeParam customAudienceLookalikeParam = new CustomAudienceLookalikeParam();
    AccountQueueWebRequestor accountQueueWebRequestor = new AccountQueueWebRequestor();

    @Test
    public void test(){
        customAudienceLookalikeParam.setName("My lookalike audience");
        customAudienceLookalikeParam.setOriginAudienceId(6035448472188l);
        List<LookalikeSpec> lookalikeSpecList = new ArrayList<LookalikeSpec>();
        LookalikeSpec lookalikeSpec = new LookalikeSpec();
        lookalikeSpec.setType("similarity");
        lookalikeSpec.setCountry("US");
        lookalikeSpec.setRatio(0.20d);
        lookalikeSpecList.add(lookalikeSpec);
//        customAudienceLookalikeParam.setLookalikeSpecList(lookalikeSpecList);
        String uri = "/act_871859516219993/customaudiences";
        List list = facebookParamAdapter.adapterParam(customAudienceLookalikeParam);
        Parameter[] parameters = (Parameter[]) list.toArray(new Parameter[list.size()]);
        String result = this.makePostRequest(uri, token, null, parameters);
        System.out.println(result);
    }


    public String makePostRequest(String endpoint, String accessToken, List<BinaryAttachment> binaryAttachments, Parameter[] parameters) {
        DefaultFacebookClient facebookClient = getFacebookClient(accessToken);
        return facebookClient.makeRequest(endpoint, true, false, binaryAttachments, parameters);
    }

    protected DefaultFacebookClient getFacebookClient(String accessToken) {
        return new DefaultFacebookClient(accessToken, accountQueueWebRequestor, new DefaultJsonMapper(), VERSION);
    }
}
